home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1096 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: news.lpr.carel.fi!usenet
  2. From: aril@cmt.lpr.mail.carel.fi (Ari Lukumies)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Trouble with file return code.
  5. Date: Thu, 11 Jan 1996 12:42:15 GMT
  6. Organization: Carelcomp Forest Oy
  7. Message-ID: <4d30vn$k8r@tahko.lpr.carel.fi>
  8. References: <tcpnntpd.16.1.10.20.22.42.2781597121.333610@the-fix.sos.on.ca>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. <xenon@the-fix.sos.on.ca> wrote:
  13.  
  14. >       Im having some trouble getting fread to return NULL when it can't
  15. >find a specific file...
  16.  
  17. >         else if((ptr=fread(filename1,"rb"))!=NULL)
  18. >  {
  19. >       //do this }
  20. >         else    {
  21. >       // do that
  22. >       // this never executes, even if NULL is returned.
  23. >No matter the file, Null is never returned.  Although, when the variable
  24. >ptr is outputed, the value shown is "0".
  25. >thankx
  26. >xenon@the-fix.sos.on.ca
  27.  
  28. You've got things a little confused here. fread is used to READ the
  29. contents of an opened file. fopen is the one you use to open the file
  30. first, fclose to close it. For example:
  31.  
  32.     ...
  33.     FILE    *fp;
  34.  
  35.     fp = fopen(filename, "rb");
  36.     if (!fp)
  37.         perror(filename);    /* File open failed */
  38.     while (fread(buffer, sizeof(buffer), 1, fp) == 1)
  39.         ProcessBuffer(buffer);
  40.     if (feof(fp))
  41.         ; /* Just reached end of file */
  42.     else if (ferror(fp))
  43.         perror(filename);    /* Some read error occurred */
  44.     fclose(fp);
  45.  
  46. Just keep on looking through your manuals...
  47.  
  48. Later,
  49.  AriL
  50. All my opinions are mine and mine alone.
  51.  
  52.